//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.packet.PacketProblemData;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PanelQuestionBase extends VerticalPanel {
	private HorizontalPanel panelProblemInfo;
	private WidgetInputBase widgetInput;
	private final SimplePanel panelTimer = new SimplePanel();
	private String stringRatio;
	private final Label labelRatio = new Label("正解率:???");

	protected PanelQuestionBase() {
	}

	protected void setPanel(PacketProblemData problem, WidgetProblemSentenceBase problemStatement, WidgetAnswer answer, WidgetInputBase input) {
		setHorizontalAlignment(ALIGN_CENTER);

		String genre = Constant.NAMES_OF_GENRE[problem.genre];
		String type = Constant.NAMES_OF_TYPE_WITH_RANDOM_FLAG[problem.type];
		if (problem.good + problem.bad == 0) {
			stringRatio = "正解率:New";
		} else if (problem.good + problem.bad < Constant.MAX_RATIO_CALCULATING) {
			stringRatio = "正解率:集計中";
		} else {
			stringRatio = "正解率:" + ((problem.good * 100) / (problem.good + problem.bad)) + "%";
		}

		panelProblemInfo = new HorizontalPanel();
		panelProblemInfo.addStyleName("gwt-HorizontalPanel-problemInformation");
		panelProblemInfo.add(new Label(genre));
		panelProblemInfo.add(new Label(type));
		panelProblemInfo.add(labelRatio);

		// panelProblemInfo.add(new Label("問題番号:" + problemId));
		if (problem.creator != null && problem.creator.length() != 0) {
			panelProblemInfo.add(new Label("作者:" + problem.creator));
		}

		widgetInput = input;

		add(panelProblemInfo);
		add(problemStatement);
		add(panelTimer);
		if (answer != null) {
			add(answer);
		}
		add(input);

		setWidth("600px");

		setCellHeight(input, "300px");
	}

	public WidgetInputBase getWidgetInput() {
		return widgetInput;
	}

	public void setWidgetTimer(WidgetTimer timer) {
		panelTimer.clear();
		panelTimer.setWidget(timer);
	}

	public void setInputEnable(boolean enable) {
		widgetInput.enable(enable);
	}

	public void displayRatio() {
		labelRatio.setText(stringRatio);
	}
}
